import java.io.*;

/**
 * @author 35326
 * @date 2021/3/3 15:41
 */
public class MyClassLoader extends ClassLoader{
    private final String byteCodePath;

    public MyClassLoader(String byteCodePath) {
        this.byteCodePath = byteCodePath;
    }

    public MyClassLoader(ClassLoader parent, String byteCodePath) {
        super(parent);
        this.byteCodePath = byteCodePath;
    }

    @Override
    protected Class<?> findClass(String name) {
        String path=byteCodePath+name+".class";
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        BufferedInputStream bufferedInputStream = null;
        try {
            bufferedInputStream = new BufferedInputStream(new FileInputStream(path));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        byte[] bytes = new byte[1024];
        int len=-1;
        try{
            while((len=bufferedInputStream.read(bytes))!=-1){
                byteArrayOutputStream.write(bytes,0,len);
            }
        }catch (IOException e){

        }




        Class c=defineClass(null,byteArrayOutputStream.toByteArray(),0,byteArrayOutputStream.toByteArray().length);

        try {
            byteArrayOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            bufferedInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return c;

    }
}
